<!-- 维修管理 -->
<template>
  <div class="mod-config">
      <el-button-group style="margin-bottom: 10px;">
        <el-button v-if="permissions.eims_repairs_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button type="primary" @click="searchReset">清空</el-button>
      </el-button-group>
      <el-form inline @keyup.enter.native="searchHandle">
        <el-form-item label="维修项">
          <el-select v-model="dataForm.repairConfigId" placeholder="请选择">
            <el-option v-for="item in repairConfig" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修部门">
          <el-select v-model="dataForm.workDeptId" placeholder="请选择">
            <el-option v-for="item in deptOptions" :key="item.value" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发起时间">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.createTimeStart" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.createTimeEnd" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
          <el-table-column prop="customerName" label="顾客姓名"></el-table-column>
          <el-table-column prop="repairConfigId" label="维修项"></el-table-column>
          <el-table-column prop="customerTel" label="联系电话"></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="workTime" label="维修时间"></el-table-column>
          <el-table-column prop="takeMirroTime" label="顾客取件时间"></el-table-column>
          <el-table-column prop="name" label="备注"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-if="permissions.eims_repairs_edit && scope.row.repairStatus == '3'" type="text" icon="el-icon-edit" @click="customerTakePart(scope.row.id)">顾客取件</el-button>
              <el-button v-else-if="permissions.eims_repairs_edit" type="text" disabled icon="el-icon-edit">顾客取件</el-button>
              <el-button v-if="permissions.eims_repairs_edit &&  scope.row.repairStatus == '1'" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-else-if="permissions.eims_repairs_edit" type="text" disabled icon="el-icon-edit">修改</el-button>
              <!-- <el-button v-if="permissions.eims_repairs_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          background
          layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import {mapGetters} from 'vuex'
  import { getRepairList, repairCustomPick } from '@/api/eims/repair'
  import { fetchRepairList } from '@/api/eims/repair-config'
  import { getDeptInfoByDeptTypes } from '@/api/admin/dept.js'

  import TableForm from './form/AddForm'

  export default {
    data () {
      return {
        dataForm: {},
        repairConfig: [],
        deptOptions: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created () {
      this.getDataList()
      this.getRepairConfig()
      this.getDeptList()
    },
    computed: {
      ...mapGetters(['permissions','userInfo']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      searchHandle(){
        this.pageIndex = 1
        this.getDataList()
      },
      searchReset(){
        for(let key in this.dataForm){
          this.dataForm[key] = ''
        }
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        getRepairList(Object.assign({ current: this.pageIndex, size: this.pageSize },this.dataForm)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        }).catch((err) => {
          this.dataListLoading = false
          this.$message.error(err);
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,this.repairConfig,this.deptOptions)
        })
      },
      // 删除
      // deleteHandle (id) {
      //   this.$confirm('是否确认删除ID为' + id, '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(function () {
      //     return delAddFeeObj(id)
      //   }).then(response => {
      //     if(response.data.code == '0'){
      //       this.$message.success('删除成功')
      //     }else{
      //       this.$message.error(response.data.msg)
      //     }
      //     this.getDataList()
      //   })
      // },
      getRepairConfig(){
        fetchRepairList({current: 1, size: 100}).then(response => {
          if(response.data.code == '0'){
            this.repairConfig = response.data.data.records
          }
        }).catch((err) => {
          this.$message.error(err);
        })
      },
      // 获取维修加工部门
      getDeptList(){
        let data = {
          deptTypes : [2,8]
        }
        getDeptInfoByDeptTypes(data).then(result => {
          let dataList = result.data.data
          let options = []
          dataList.forEach(item => {
            options.push({value:item.id,name:item.name})
          })
          this.deptOptions = options
        })
      },
      // 顾客取镜
      customerTakePart(id){
        repairCustomPick(id).then(response => {
          if(response.data.code == '0'){
            this.$message.success('顾客取件成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        }).catch((err) => {
          this.$message.error(err)
        })
      }
    }
  }
</script>
